<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" @close="close" width="500px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="注册号" prop="registerCode">
        <el-input @blur="studetnNoChange" v-model="form.registerCode" placeholder="请输入注册号" />
      </el-form-item>
      <el-form-item label="姓名" prop="studentName">
        <el-input disabled v-model="form.studentName" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="宿舍号" prop="roomNo">
        <el-input disabled v-model="form.roomNo" placeholder="请输入宿舍号" />
      </el-form-item>
      <el-form-item label="违纪类型" prop="violateType">
        <el-select v-model="form.violateType" placeholder="请选择违纪类型" style="width: 100%">
          <el-option v-for="dict in dict.type.dorm_violate_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="违纪时间" prop="violateTime">
        <el-date-picker v-model="form.violateTime" type="datetime" placeholder="选择违纪时间"
          value-format="yyyy-MM-dd HH:mm:ss" />
      </el-form-item>
      <el-form-item label="处理状态" prop="delFlag">
        <el-select v-model="form.delFlag" placeholder="请选择处理状态" style="width: 100%">
          <el-option v-for="item in delFlagOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="违纪事实" prop="violateContent">
        <el-input v-model="form.violateContent" type="textarea" placeholder="请输入违纪事实" />
      </el-form-item>
      <el-form-item label="处理结果" prop="violateResult">
        <el-input v-model="form.violateResult" type="textarea" placeholder="请输入违纪处理反馈结果" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDormAllocationInfolist } from '@/api/apartment/roomBedlist'
export default {
  name: "AddForm",
  dicts: ['dorm_violate_type'],
  props: {
    title: {
      type: String,
      default: ''
    },
    open: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {
      dialogVisible: false,
      delFlagOptions: [
        { label: '未处理', value: '0' },
        { label: '已删除', value: '2' }
      ],
      form: {
        id: undefined,
        registerCode: undefined,
        studentName: undefined,
        roomNo: undefined,
        violateType: undefined,
        violateTime: undefined,
        delFlag: '0',
        violateContent: undefined,
        violateResult: undefined,
        remark: undefined
      },
      rules: {
        registerCode: [
          { required: true, message: "学号不能为空", trigger: "blur" }
        ],
        studentName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        roomNo: [
          { required: true, message: "宿舍号不能为空", trigger: "blur" }
        ],
        violateType: [
          { required: true, message: "违纪类型不能为空", trigger: "change" }
        ],
        violateTime: [
          { required: true, message: "违纪时间不能为空", trigger: "change" }
        ],
        delFlag: [
          { required: true, message: "处理状态不能为空", trigger: "change" }
        ]
      }
    };
  },
  watch: {
    open: {
      handler(val) {
        this.dialogVisible = val;
      },
      immediate: true
    },
    dialogVisible(val) {
      if (!val) {
        this.$emit('close');
      }
    },
    
  },
  methods: {
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$emit('submit', this.form)
        }
      });
    },
    cancel() {
      this.$emit('close')
    },
    studetnNoChange(val) {
      getDormAllocationInfolist({
        // registerCode: this.form.registerCode
        studentNo: this.form.registerCode
      }).then(res => {
        if(res.rows.length > 0) {
          this.form.studentName = res.rows[0].studentname
          this.form.roomNo = res.rows[0].roomNumber
          this.form = {...res.rows[0],...this.form}
        }
        // this.form.roomNo = res.data.roomNo
      })
    },
  }
};
</script>